﻿@using Kendo.Mvc.UI;
@{
    ViewBag.Title = "Thêm/Sửa Quyền";
}
@{
    AjaxOptions aj = new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "content-main",
        LoadingElementId = "loading-icon",
        OnFailure = "AjaxFailure",
        OnBegin = "AjaxBegin",
        OnComplete = "AjaxComplete"
    };
}
@model List<TS2015.Models.TbChucNang>
@using (@Html.BeginForm("AddActing", "AccoutManagement", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <section class="panel">
        <header class="panel-heading">
            <h4 style="margin:0">Thêm/Sửa quyền</h4>
        </header>
        <div class="panel-body">
            <div class="table-responsive">
                <div class="form-group">
                    <div class="col-lg-6 col-md-6 row">
                        <strong class="col-lg-3 col-md-3 row">Tên quyền:</strong>
                        <div class="col-lg-6 col-md-6 row">
                            @if (ViewBag.IDRequest != null)
                            {
                                @Html.TextBox("txtActing", (string)ViewBag.Acting, new { @class = "form-control", disabled = "disabled" })
                                <input type="hidden" name="MyActing" value="@ViewBag.Acting" />
                                <input type="hidden" name="IDActing" value="@ViewBag.IDRequest" />
                            }
                            else
                            {
                                @Html.TextBox("txtActing", "", new { @class = "form-control valid", data_val = "true", data_val_required = " ", id = "txtActing", aria_required = "true", aria_invalid = "true", aria_describedby = "txtActing-error" })
                            }
                            @Html.ValidationMessage("txtActing")
                        </div>
                        <div class="clear"></div>
                        <strong class="col-lg-3 col-md-3 row">Trạng thái:</strong>
                        <div class="col-lg-6 col-md-6 row">
                            @Html.DropDownList("Value", (SelectList)ViewBag.TrangThai, "Chọn...", new { @class = "form-control valid", data_val = "true", data_val_required = " ", aria_required = "true", aria_describedby = "Value-error Value-error Value-error" })
                            @Html.ValidationMessage("Value")

                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 row">
                        <div class="col-lg-3 col-md-3 row">
                            <strong>Chức năng: </strong>
                        </div>
                        <div style="clear:both"></div><br />
                        <div class="col-lg-12 col-md-12 row">
                            <div class="treeview-back">
                                @(Html.Kendo().TreeView()
                            .Name("treeview")
                            .Checkboxes(checkboxes => checkboxes
                                .Name("checkedFiles")
                                .CheckChildren(true)
                            )
                            .Items(treeview =>
                            {

                                //int count = 0;
                                var listParent = Model.Where(m => m.MaCNCha == 0 && m.TrangThai == 1 && m.IsMenu==true).OrderBy(m => m.ThuTu);
                                foreach (var parent in listParent)
                                {
                                    if (parent.MaCNCha == 0)
                                    {
                                        //count = count + 1;
                                        treeview.Add().Text(parent.TenCN).Id(parent.MaCN.ToString())
                                        .Expanded(false)

                                        .Items(root =>
                                        {
                                            var listChildren = Model.Where(m => m.MaCNCha > 0 && m.MaCNCha == parent.MaCN && m.TrangThai == 1 && m.IsMenu == true).OrderBy(m => m.ThuTu);
                                            foreach (var item in listChildren)
                                            {
                                                root.Add().Text(item.TenCN).Id(item.MaCN.ToString())
                                                .Expanded(false)
                                                .Items(items =>
                                                {
                                                    var listNephew = Model.Where(m => m.MaCNCha == item.MaCN && m.IsMenu == false).OrderBy(m => m.ThuTu);
                                                    foreach (var funtion in listNephew)
                                                    {
                                                        items.Add().Text(funtion.TenCN).Id(funtion.MaCN.ToString());
                                                    }
                                                });
                                            }
                                        });
                                    }
                                }
                            })
                            .ItemAction(item =>
                            {
                                if (ViewBag.Funtion == null)
                                {
                                    item.Checked = false;
                                }
                                else
                                {
                                    item.Checked = ((int[])ViewBag.Funtion).Contains(int.Parse(item.Id));
                                }

                            })
                                )
                                @Html.ValidationMessage("treeview")
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            <div style="clear:both"></div><br />
            <div style="clear:both"></div>
            <div class="form-group">
                <div class="col-lg-12 col-md-12">
                    <div class="toolbar" style="float:left">
                        @if (ViewBag.IDRequest != null)
                        {
                            <button class="btn btn-danger btn-primary-registration" type="submit"><b>Sửa</b></button>
                        }
                        else
                        {
                            <button class="btn btn-danger btn-primary-registration" type="submit"><b>Thêm mới</b></button>
                        }
                        <a class="btn btn-danger btn-primary-registration" href="/Admin/AccoutManagement/Acting"><b>Quay lại</b></a>
                    </div>
                </div>
            </div>
        </div>

    </section>
}